#include <func.h>

pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

typedef struct
{
    int id;
    int money;
}Account;

Account acct1 = {1,100};

void* getmoney(void*)
{
    if(acct1.money >= 100)
    {
        sleep(1);
        acct1.money -= 100;
        printf("get money 100,cout money %d\n",acct1.money);

        pthread_mutex_unlock(&mtx);
        return NULL;
    }

    printf("NO money: %d\n",acct1.money);
    pthread_mutex_unlock(&mtx);
    return NULL;
}

int main(int argc,char* argv[])
{

    pthread_t tid[10];
    for(int i = 0;i<10;i++)
    {
        pthread_mutex_lock(&mtx);

        pthread_create(&tid[i],NULL,getmoney,NULL);
        
    }

    for(int i = 0;i<10;i++)
        pthread_join(tid[i],NULL);

    return 0;
}
